package com.qianxx.component.activity.recycleview.slide_recycler_view;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.component.activity.recycleview.pull_recyclerview.LoadMoreView;
import com.qianxx.component.activity.recycleview.pull_recyclerview.RefreshHeadView;
import com.qianxx.viewlibrary.recyclerview.pull.PullListener;
import com.qianxx.viewlibrary.recyclerview.slide.SlideRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SlideRecyclerViewActivity extends BaseActivity implements PullListener,
        SlideRecyclerViewAdapter.OnItemDeleteListener {

    @BindView(R.id.rv)
    SlideRecyclerView rv;

    SlideRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_recycler_view);
        ButterKnife.bind(this);

        adapter = new SlideRecyclerViewAdapter(this);
        adapter.setOnItemRemoveListener(this);
        rv.setHeadRefreshView(new RefreshHeadView(this));
        rv.setMoreRefreshView(new LoadMoreView(this));
        rv.setUseLoadMore(true);
        rv.setUseRefresh(true);
        rv.setPullLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
        rv.setPullListener(this);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("");
        }
        adapter.setAll(list);
    }

    @Override
    public String setTitle() {
        return "SlideRecyclerViewAdapter";
    }

    @Override
    public void onDelete(int position, String entity) {
        adapter.remove(position);
        rv.closeMenu();
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rv.onRefreshComplete();
            }
        }, 2000);
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rv.onLoadMoreComplete();
            }
        }, 2000);
    }
}
